package com.sysu.dinner.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.dinner.model.Manager;
import com.sysu.dinner.model.Restaurant;
import com.sysu.dinner.service.ManagerService;
import com.sysu.dinner.service.RestaurantService;

@Component("managerRegisterAction")
@Scope("prototype")
public class ManagerRegisterAction extends ActionSupport{

	private static final long serialVersionUID = 5794709591273931065L;
	
	private String name;
	private String password;
	private String password2;
	private String phone;
	private String restautantName;
	private File imgPath;
	
	private String savePath;

	private ManagerService managerService;
	private RestaurantService restaurantService;
	
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=GBK");
		PrintWriter out = response.getWriter();
		
		Manager manager = new Manager();
		manager.setName(name);
		manager.setPassword(password);
		manager.setPhoneNumber(phone);
		
		Restaurant restaurant = new Restaurant();
		restaurant.setName(restautantName);
		restaurant.setManager(manager);
		//restaurant.setImgPath(getSavePath());
		
		if(managerService.isExist(name, phone))
		{
			out.println(1);
			return null;
		}
		
		if(managerService.addManager(manager)>0 && restaurantService.addRestaurant(restaurant)>0)
		{
			//writeToBasePath();
			HttpSession session = ServletActionContext.getRequest().getSession();;
			session.setAttribute("user", name);
			session.setAttribute("role", "manager");
			session.setAttribute("phone", phone);
			out.println(2);
			return null;
		}
		
		out.println(0);
		return null;
	}
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Resource
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

	public ManagerService getManagerService() {
		return managerService;
	}

	public void setRestautantName(String restautantName) {
		this.restautantName = restautantName;
	}

	public String getRestautantName() {
		return restautantName;
	}

	@Resource
	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}
	
	public File getImgPath() {
		return imgPath;
	}

	public void setImgPath(File imgPath) {
		this.imgPath = imgPath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getSavePath() {
		String path = imgPath.getPath();
		String[] subpaths = path.split(".");
		String type = subpaths[subpaths.length-1];
		return ServletActionContext.getServletContext().getRealPath("/img/"+restautantName+"_"+phone+"."+type);
	}


}
